function op(type) {
  return function(limit) {
    var numElem = $(".cheerup").find(".num").find("input");
    var num = parseInt(numElem.val().trim()) || 0;
    if (type == "+") {
      if (limit != undefined && (num >= limit)) return;
      num++;
    } else if (type == "-") {
      if (limit != undefined && (num <= limit)) return;
      num--;
    }

    numElem.val(num);
  }
}

var jia = op("+");
var jian = op("-");

function energyDialog(avatar, name, amount, coins) {
  $(".cheerup").find(".avatar").css({"backgroundImage": "url(" + avatar + ")"}).end()
               .find(".name").text(name).end()
               .find(".num").find("input").val(amount).end()
               .find(".coins").find("span").text(coins);
  dialog(".cheerup");
}

function avatar() {
  //调整头像样式
  var rankingItems = $(".ranking li");
  var avatars = rankingItems.find(".avatar");

  avatars.each(function() {
    if($(this).height() > rankingItems.height()) {
      var limit = rankingItems.height() - 8;
      $(this).width(limit).height(limit);
    }
  })
}
avatar()
/*
//防止快速点击延迟
$(function() {
  FastClick.attach(document.body);
});*/

var tabEnterTime = 0;
$(".jian").on("touchstart mousedown", function(e) {
  e.stopPropagation()
  e.preventDefault()
  tabEnterTime = Date.now();
}).on("touchend mouseup", function() {
  if (Date.now() - tabEnterTime < 1000) {
    jian(0);
  }
})

$(".jia").on("touchstart mousedown", function(e) {
  e.stopPropagation()
  e.preventDefault()
  tabEnterTime = Date.now();
}).on("touchend mouseup", function() {
  if (Date.now() - tabEnterTime < 1000) {
    jia();
  }
})

var UL_H = $(window).height() - $(".ranking").offset().top - 27;
var LI_H = UL_H / ($(".ranking").find("li").size());

function justifyLi() {
  if($(".ranking").height() < UL_H) {
    $(".ranking").find("li").each(function() {
      $(this).height(LI_H);
    })
  }
}

/*justifyLi()*/